package org.ybxb.demo.config;

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.format.DateTimeFormatter;

/**
 * @ProjectName: apaas-demo
 * @PackageName: com.ctsi.ssdc.config
 * @Description:
 * @Author: hy.bai
 * @Date: 2024-02-06 10:56
 */
@Configuration
@MapperScan("org.ybxb.demo.**.mapper")
public class AppConfig {

    /*@Bean
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new
        MappingJackson2HttpMessageConverter();
        JsonMapper.Builder builder = JsonMapper.builder(); //主要是依赖于ObjectMapper来进行实现
        // ObjectMapper objectMapper = new ObjectMapper();
        // objectMapper.setDateFormat();
        // SimpleModule simpleModule = new SimpleModule();
        // objectMapper.registerModule(simpleModule);
        builder.defaultDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        mappingJackson2HttpMessageConverter.setObjectMapper(builder.build());
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.valueOf
        ("application/json;charset=UTF-8")));
        return mappingJackson2HttpMessageConverter;
    }*/


    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            // 设置 LocalDate和LocalDateTime反序列化器
            builder.deserializers(new LocalDateDeserializer(dateFormatter));
            builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            // 设置 LocalDate和LocalDateTime序列化器
            builder.serializers(new LocalDateSerializer(dateFormatter));
            builder.serializers(new LocalDateTimeSerializer(dateTimeFormatter));
        };
    }

}
